﻿#include "BrickEx.h"
#include "ResourceManager.h"
#include <d3dx9.h>


BrickEx::BrickEx(int id,float x, float y, int state, int type)
{
	_id = id;
	_x = x;
	_y = y;
	_sprite = new Sprite(_spriteHandler, ResourceManager::GetInstance()->GetTexture(ResourceManager::GetInstance()->TextureName.Brick),0, 0);

	_width = _sprite->getTexture()->getWidth();
	_height = _sprite->getTexture()->getHeight();

	_vx = 0;
	_vy = 0;
	_xPre = x;
	_yPre = y;

	_accelY = 0;
	isBreaked = false;

	isLastMoveUp = false;

	_spriteArr[0] = new Sprite(_spriteHandler, ResourceManager::GetInstance()->GetTexture(ResourceManager::GetInstance()->TextureName.Brick_Explosion),0, 0);
	_spriteArr[1] = new Sprite(_spriteHandler, ResourceManager::GetInstance()->GetTexture(ResourceManager::GetInstance()->TextureName.Brick_Explosion),0, 0);
	_spriteArr[2] = new Sprite(_spriteHandler, ResourceManager::GetInstance()->GetTexture(ResourceManager::GetInstance()->TextureName.Brick_Explosion),0, 0);
	_spriteArr[3] = new Sprite(_spriteHandler, ResourceManager::GetInstance()->GetTexture(ResourceManager::GetInstance()->TextureName.Brick_Explosion),0, 0);

	_state = BS_NORMAL;
	_type = type;
}

BrickEx::~BrickEx()
{

}


void BrickEx::Render(int cameraX, int cameraY)
{
	switch(_state)
	{
	case BS_NONE:
	case BS_NORMAL:
	case BS_MOVEUP:
		_sprite->Render(_x, _y, cameraX, cameraY);
		break;
	case BS_BREAKING:
		_spriteArr[0]->Render(xBreak1, yBreak1, cameraX, cameraY); 
		_spriteArr[1]->Render(xBreak2, yBreak2, cameraX, cameraY);
		_spriteArr[2]->Render(2 * _x - xBreak1, yBreak1, cameraX, cameraY);
		_spriteArr[3]->Render(2 * _x - xBreak2, yBreak2, cameraX, cameraY);
		break;
	}
}


void BrickEx::Update(list<Object*>* obj_list, float gameTime)
{
	list<Object*>::iterator i;
	Object* obj;

	switch(_state)
	{
	case BS_NONE:

		_sprite->Update(gameTime);
		break;
	case BS_NORMAL:

		_sprite->Update(gameTime);
		break;
	case BS_MOVEUP:

		_vy -= _accelY * gameTime;
		_y += _vy * gameTime + 1.0f/2.0f * _accelY * gameTime * gameTime;

		//Xu li dung gach voi coin,...
		for (i = obj_list->begin(); i != obj_list->end(); i++)
		{
			obj = *i;


			//Box box = this->getBox(time);
			Box box(this->getPosX() + _width/2,
				this->getPosY() - this->getHeight(), 
				this->getWidth()/2, 
				this->getHeight(), 
				0, 
				_vy * gameTime + 1.0f/2.0f * _accelY * gameTime * gameTime);

			//Box block = obj->getBox(time);
			Box block(obj->getPosX() + obj->getWidth()/2,
				obj->getPosY() - obj->getHeight(),// - 0.11,
				obj->getWidth()/2,
				obj->getHeight()
				);

			Box boxphase = Collision::_GetSweptBroadphaseBox(box);
			if (Collision::_AABBCheck(boxphase, block))
			{
				switch (obj->getIDObjType())
				{
				case 39:
					if (obj->getObjState() == IS_IDLE)
					{
						obj->changeState(IS_ACTIVING);
						int abc= 1;
						
					}

				case 11:		
				case 12:		
					obj->changeState(ES_FALL);
					break;


				}
				
			}
		}

		if (_y <= _yPre)
		{
			changeState(BS_NORMAL);
		}

		_sprite->Update(gameTime);
		//}
		break;
	case BS_BREAKING:
		xBreak1 += vxBreak1 * gameTime;
		xBreak2 += vxBreak2 * gameTime;

		vyBreak1 -= ayBreak * gameTime;
		vyBreak2 -= ayBreak * gameTime;

		yBreak1 += vyBreak1 * gameTime + 1.0f/2.0f * ayBreak * gameTime * gameTime;
		yBreak2 += vyBreak2 * gameTime + 1.0f/2.0f * ayBreak * gameTime * gameTime;


		//Xu li dung gach voi coin,...
		for (i = obj_list->begin(); i != obj_list->end(); i++)
		{
			obj = *i;
			//Box box = this->getBox(time);
			Box box(this->getPosX() + _width/2,
				this->getPosY() - this->getHeight(), 
				this->getWidth()/2, 
				this->getHeight(), 
				0, 
				vyBreak1 * gameTime + 1.0f/2.0f * ayBreak * gameTime * gameTime);

			//Box block = obj->getBox(time);
			Box block(obj->getPosX() + obj->getWidth()/2,
				obj->getPosY() - obj->getHeight(),// - 0.11,
				obj->getWidth()/2,
				obj->getHeight()
				);

			Box boxphase = Collision::_GetSweptBroadphaseBox(box);
			if (Collision::_AABBCheck(boxphase, block))
			{
				switch (obj->getIDObjType())
				{
				case 11:		//goomba
				case 12:		//koopba
					obj->changeState(ES_FALL);
					break;					
				}

			}
		}


		if(yBreak1 <= 0)
		{
			changeState(BS_REMOVE);
		}
		_spriteArr[0]->Update(gameTime);
		_spriteArr[1]->Update(gameTime);
		_spriteArr[2]->Update(gameTime);
		_spriteArr[3]->Update(gameTime);
		//ResetRect_NULL();
		break;
	}
	
}

void BrickEx::changeState(int state)
{
	_state = state;
	switch (_state)
	{
	case BS_NORMAL:
		_y = _yPre;
		//_vy = 0;
		_vy = 0;

		_accelY = 0;
		break;
	case BS_MOVEUP:

		_vy = 1.0;
		//_accelY = GRAVITY;
		_accelY = BRICK_GRAVITY;
		break;
	case BS_BREAKING:
		xBreak1 = xBreak2 = _x + 40/2;
		yBreak1 = _y;
		yBreak2 = _y + 40;

		vyBreak1 = 1.0;
		vyBreak2 = 0.8;

		vxBreak1 = 0.150;
		vxBreak2 = 0.100;
		ayBreak = BRICK_GRAVITY;
		break;
	case BS_NONE:
		//_sprite->setFrame(1,1);
		isLastMoveUp = true;
		break;
	case BS_REMOVE:
		vxBreak1 = vxBreak2 = 0;
		vyBreak1 = vyBreak2 = 0;
		ayBreak = 0;
		break;
	}
}

OBJECT_TYPE BrickEx::getObjectType()
{
	return OBJECT_LAND;
}